﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using TA.DotNetUtils;

namespace TA.Turbo.Controls
{
	public abstract class BaseRepeaterControl : Control
	{
		public BaseRepeaterControl(Repeater repeater)
		{
			this._repeater = repeater;
		}
		public Repeater Repeater
		{
			get { return this._repeater; }
		}
		Repeater _repeater;
	}
	public class RepeaterPagerContainer : BaseRepeaterControl
	{
		public RepeaterPagerContainer(Repeater rpt, string cssClass, PagerPosition position)
			: base(rpt)
		{
			this._cssClass = cssClass;
			this._position = position;

			this.Controls.Add(new RepeaterPageButtonFirst(this.Repeater));
			this.Controls.Add(new RepeaterPageButtonPrevious(this.Repeater));
			this.Controls.Add(new RepeaterPageButtonPreviousGroup(this.Repeater));

			this.Controls.Add(new RepeaterPagesButtonsContainer(this.Repeater));

			this.Controls.Add(new RepeaterPageButtonNextGroup(this.Repeater));
			this.Controls.Add(new RepeaterPageButtonNext(this.Repeater));
			this.Controls.Add(new RepeaterPageButtonLast(this.Repeater));
		}
		public override void RenderControl(HtmlTextWriter writer)
		{
			int minNumPages = (int)this.Repeater.Pager.DisplayMode;

			if (this.Repeater.Pager.Size == 0 || // no pagin or ...
				this.Repeater.NumPages < minNumPages || // ... not enough pages or ...
				(this.Repeater.Pager.Position & this._position) != this._position // ... this pager is not visible
				)
				writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
			writer.AddAttribute(HtmlTextWriterAttribute.Class, "pager " + this._cssClass);
			writer.RenderBeginTag(HtmlTextWriterTag.Div);


			this.RenderChildren(writer);


			writer.RenderEndTag();
		}


		string _cssClass;
		PagerPosition _position;
	}
	public abstract class RepeaterPageButton : BaseRepeaterControl
	{
		internal RepeaterPageButton(Repeater repeater)
			: base(repeater)
		{
		}
		public override void RenderControl(HtmlTextWriter writer)
		{
			int displayIndex = this.Index + 1;

			string saveText = this.PagerElement.Text,
				saveImageSrc = this.PagerElement.ImageSrc,
				saveImageAlt = this.PagerElement.ImageAlt;

			this.PagerElement.Text = (this.PagerElement.Text ?? "").Replace("###", displayIndex.ToString());
			this.PagerElement.ImageSrc = (this.PagerElement.ImageSrc ?? "").Replace("###", displayIndex.ToString());
			this.PagerElement.ImageAlt = (this.PagerElement.ImageAlt ?? "").Replace("###", displayIndex.ToString());

			this.AddAttributesToRender(writer);
			writer.RenderBeginTag(HtmlTextWriterTag.A);

			this.PagerElement.Render(writer);

			writer.RenderEndTag();




			this.PagerElement.Text = saveText;
			this.PagerElement.ImageSrc = saveImageSrc;
			this.PagerElement.ImageAlt = saveImageAlt;
		}

		protected virtual void AddAttributesToRender(HtmlTextWriter writer)
		{
			if (!this.Selected)
			{
				string href = this.PagerElement.Href, hrefJs = this.PagerElement.HrefJS;
				if (string.IsNullOrEmpty(href))
					href = this.PagerSettings.ButtonHref;
				if (string.IsNullOrEmpty(hrefJs))
					hrefJs = this.PagerSettings.ButtonHrefJS;
				if (!string.IsNullOrEmpty(href) || !string.IsNullOrEmpty(hrefJs))
				{
					Link.AddHrefAttribute(
						writer,
						this,
						(href ?? "").Replace("###", (this.Index + 1).ToString()),
						(hrefJs ?? "").Replace("###", (this.Index + 1).ToString()));
				}
				else
					writer.AddAttribute(HtmlTextWriterAttribute.Href, " ");
			}
			if (!(this.PagerElement.Visible))
				writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
			if (!string.IsNullOrEmpty(this.CssClass))
				writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
		}
		public PagerSettings PagerSettings
		{
			get
			{
				return this.Repeater.Pager;
			}
		}
		public virtual bool Selected
		{
			get
			{
				return this.Index == this.Repeater.ActualPageIndex;
			}
		}
		public abstract PagerElement PagerElement { get; }
		public abstract int Index { get; }
		public abstract string CssClass { get; }
	}
	public class RepeaterPageButtonFirst : RepeaterPageButton
	{
		internal RepeaterPageButtonFirst(Repeater repeater)
			: base(repeater)
		{

		}
		public override PagerElement PagerElement
		{
			get { return this.PagerSettings.First; }
		}
		public override int Index
		{
			get { return 0; }
		}

		public override string CssClass
		{
			get { return "first"; }
		}
	}
	public class RepeaterPageButtonPrevious : RepeaterPageButton
	{
		internal RepeaterPageButtonPrevious(Repeater repeater)
			: base(repeater)
		{

		}
		public override PagerElement PagerElement
		{
			get { return this.PagerSettings.Previous; }
		}
		public override int Index
		{
			get { return Math.Max(0, this.Repeater.ActualPageIndex - 1); }
		}

		public override string CssClass
		{
			get { return "previous"; }
		}
	}
	public abstract class BaseRepeaterPageButtonGroup : RepeaterPageButton
	{
		public BaseRepeaterPageButtonGroup(Repeater repeater)
			: base(repeater)
		{

		}
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);

			if (this.Selected)
				writer.AddStyleAttribute(HtmlTextWriterStyle.Visibility, "hidden");
		}
	}
	public class RepeaterPageButtonPreviousGroup : BaseRepeaterPageButtonGroup
	{
		internal RepeaterPageButtonPreviousGroup(Repeater repeater)
			: base(repeater)
		{

		}
		public override PagerElement PagerElement
		{
			get { return this.PagerSettings.PreviousGroup; }
		}
		public override int Index
		{
			get { return Math.Max(0, this.Repeater.ActualPageIndex - this.PagerSettings.ButtonCount); }
		}
		public override bool Selected
		{
			get
			{
				return this.Repeater.PagesRange.Start == 0;
			}
		}
		public override string CssClass
		{
			get { return "previous_group"; }
		}
	}
	public class RepeaterPageButtonNextGroup : BaseRepeaterPageButtonGroup
	{
		internal RepeaterPageButtonNextGroup(Repeater repeater)
			: base(repeater)
		{

		}
		public override PagerElement PagerElement
		{
			get { return this.PagerSettings.NextGroup; }
		}
		public override int Index
		{
			get { return Math.Min(this.Repeater.NumPages - 1, this.Repeater.ActualPageIndex + this.PagerSettings.ButtonCount); }
		}
		public override bool Selected
		{
			get
			{
				return this.Repeater.PagesRange.End == this.Repeater.NumPages;
			}
		}
		public override string CssClass
		{
			get { return "next_group"; }
		}
	}
	public class RepeaterPageButtonNext : RepeaterPageButton
	{
		internal RepeaterPageButtonNext(Repeater repeater)
			: base(repeater)
		{

		}
		public override PagerElement PagerElement
		{
			get { return this.PagerSettings.Next; }
		}
		public override int Index
		{
			get { return Math.Min(this.Repeater.NumPages - 1, this.Repeater.ActualPageIndex + 1); }
		}
		public override string CssClass
		{
			get { return "next"; }
		}
	}
	public class RepeaterPageButtonLast : RepeaterPageButton
	{
		internal RepeaterPageButtonLast(Repeater repeater)
			: base(repeater)
		{

		}
		public override PagerElement PagerElement
		{
			get { return this.PagerSettings.Last; }
		}
		public override int Index
		{
			get { return this.Repeater.NumPages - 1; }
		}
		public override string CssClass
		{
			get { return "last"; }
		}
	}
	public class RepeaterPagesButtonsContainer : BaseRepeaterControl
	{
		public RepeaterPagesButtonsContainer(Repeater repeater)
			: base(repeater)
		{
			this._button = new RepeaterPageButtonNormal(repeater);
		}
		public override void RenderControl(HtmlTextWriter writer)
		{
			writer.AddAttribute(HtmlTextWriterAttribute.Class, "pages");
			writer.RenderBeginTag(HtmlTextWriterTag.Span);

			IntRange pagesRange = this.Repeater.PagesRange;
			for (int i = pagesRange.Start; i < pagesRange.End; ++i)
			{
				this._button.SetIndex(i);
				this._button.RenderControl(writer);
			}

			writer.RenderEndTag();
		}
		RepeaterPageButtonNormal _button;
	}
	public class RepeaterPageButtonNormal : RepeaterPageButton
	{
		internal RepeaterPageButtonNormal(Repeater repeater)
			: base(repeater)
		{

		}
		public override PagerElement PagerElement
		{
			get { return this.PagerSettings.Normal; }
		}
		public override int Index
		{
			get { return this._index; }
		}
		public void SetIndex(int index)
		{
			this._index = index;
		}
		public override string CssClass
		{
			get { return null; }
		}
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);

			if (this.Selected)
				writer.AddAttribute(HtmlTextWriterAttribute.Class, "selected");
		}
		int _index;
	}
}
